/*
 * ring_buffer_dma.h
 *
 *  Created on: 2024年8月16日
 *      Author: nwz
 */

#ifndef CODE_BSP_RING_BUFFER_DMA_H_
#define CODE_BSP_RING_BUFFER_DMA_H_

#include <basic_header.h>

typedef struct {
	uint32_t start_pos;
	uint32_t end_pos;

	uint32_t buffer_size;
	uint8_t *buffer;
	SemaphoreHandle_t sem;
	SemaphoreHandle_t write_sem;
	SemaphoreHandle_t read_sem;
	void *write_channel;
	void *read_channel;

} ring_buffer_handle;

typedef ring_buffer_handle *ring_buffer_handle_t;

ring_buffer_handle_t ring_buffer_create(uint32_t size, void *write_channel,
		void *read_channel);

uint32_t ring_buffer_read(ring_buffer_handle_t handle, uint8_t *data,
		uint32_t size, uint32_t timeout);

uint32_t ring_buffer_write_detail(ring_buffer_handle_t handle, uint8_t *data,
		uint32_t size,bool fromISR);

#define ring_buffer_write(handle,data,size)  			ring_buffer_write_detail(handle,data,size,false)
#define ring_buffer_write_from_isr(handle,data,size) 	ring_buffer_write_detail(handle,data,sizze,true)

void ring_buffer_clear(ring_buffer_handle_t handle);

void ring_buffer_write_cmplt_callback(ring_buffer_handle_t handle,void* dma);

void ring_buffer_read_cmplt_callback(ring_buffer_handle_t handle,void* dma);

uint32_t ring_buffer_get_available_size(ring_buffer_handle_t handle);

uint32_t ring_buffer_get_readable_size(ring_buffer_handle_t handle);

uint32_t ring_buffer_delete(ring_buffer_handle_t handle);

#endif /* CODE_BSP_RING_BUFFER_DMA_H_ */
